import time
from Timing import Timing
from TwainControl import TwainBase
import os
#import win32api
import Tkinter
import twain
import Database.Database as DB
import Database.DbFunctions as DBF
import threading

conn = DB.databaseGetConn()

#Variables
location_id = '2'
user_id = '1'
scanner_id = '2'

F=Tkinter.Frame()
window = F.winfo_id()

 
artifact_id = None
name = None
#class ScanningThread (threading.Thread):
    #def __init__(self, scanner):
        #threading.Thread.__init__(self)
       # self.scanner = scanner

   # def run ( self ):
while(1):
    scanner_name = "CanoScan 8800F"
    go = False
    raw_input("%s: Side 1" % scanner_name)
    scanner = TwainBase()
    scanner.OpenScanner(window, scanner_name)
    scanner.SD.SetCapability(twain.ICAP_YRESOLUTION, twain.TWTY_FIX32, 600.0) 
    scanner.SD.SetCapability(twain.ICAP_XRESOLUTION, twain.TWTY_FIX32, 600.0)
    print scanner.SD.GetCapability(twain.ICAP_CONTRAST)
    print scanner.SD.GetCapability(twain.ICAP_PIXELTYPE)
    scanner.SD.SetCapability(twain.ICAP_PIXELTYPE, twain.TWTY_UINT16, twain.TWPT_GRAY)
    scanner.AcquireByFile()
    watch = ''
    while(watch == ''):
        watch = scanner.PollForImage()
        time.sleep(5)
   # scanner.Terminate()
    conn.ping(True)
    cursor = DB.getCursorFromConn(conn)
    next_i = DBF.getNextNumForLocation(cursor, location_id)
    base_name = DBF.getBaseNameForLocation(cursor, location_id)
    name = "%s_%s" % (base_name, next_i)
    image_name = "%s.tiff" % name
    raw_path = "C:\\to_transfer\\%s" % image_name
    print raw_path
    os.rename('TWAIN.tmp', raw_path)
    raw_path = "raw_scans\\\%s" % image_name
    artifact_id = DBF.makeNewArtifact(conn, cursor, location_id, next_i, base_name, user_id, 0)
    DBF.addImageToArtifact(conn, cursor, artifact_id, name, raw_path, scanner_id, '1', user_id)
    conn.commit()
    #Get side 2
    go = False
    yn = raw_input("%s: Side 2" % scanner_name)
    if yn == "n":
        continue
    scanner = TwainBase()
    scanner.OpenScanner(window, scanner_name)
    scanner.SD.SetCapability(twain.ICAP_YRESOLUTION, twain.TWTY_FIX32, 300.0) 
    scanner.SD.SetCapability(twain.ICAP_XRESOLUTION, twain.TWTY_FIX32, 300.0)
    print scanner.SD.GetCapability(twain.ICAP_CONTRAST)
    print scanner.SD.GetCapability(twain.ICAP_PIXELTYPE)
    scanner.SD.SetCapability(twain.ICAP_PIXELTYPE, twain.TWTY_UINT16, twain.TWPT_GRAY)
    scanner.AcquireByFile()
    watch = ''
    while(watch == ''):
        watch = scanner.PollForImage()
        time.sleep(5)
   # scanner.Terminate()
    try:
        conn.ping(True)
    except:
        conn = DB.databaseGetConn()
    cursor = DB.getCursorFromConn(conn)
    name = name + "a"
    image_name = "%s.tiff" % name
    raw_path = "C:\\to_transfer\\%s" % image_name
    print raw_path
    os.rename('TWAIN.tmp', raw_path)
    raw_path = "raw_scans\\\%s" % image_name
    DBF.addImageToArtifact(conn, cursor, artifact_id, name, raw_path, scanner_id, '2', user_id)
    
    conn.commit()
    cursor.close()
            
#ScanningThread("CanoScan 8800F").start()
#ScanningThread("CanoScan FB630U/FB636U").start()
